﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;


namespace School.Admission
{
    public class TransferRequest
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(ADM_Transfer_Request transferrequest)
        {
            db.AddToADM_Transfer_Request(transferrequest);

            db.SaveChanges();
            return transferrequest.Id ;
        }
        public static ADM_Transfer_Request Single(long id)
        {
            try
            {
                return db.ADM_Transfer_Request.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<ADM_Transfer_Request> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from transferrequest in db.ADM_Transfer_Request select transferrequest;

            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Id")
                {
                    long Id;
                    Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Id == Id);
                }

                if (item.Key == "Payment_Order_Number")
                {
                    long payNo;
                    payNo = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Payment_Order_Number == payNo);
                }
                if (item.Key == "FirstName")
                {
                    query = query.Where(c => c.FirstName == item.Value);
                }

                if (item.Key == "SecondName")
                {
                    query = query.Where(c => c.SecondName == item.Value);
                }

                if (item.Key == "ThirdName")
                {
                    query = query.Where(c => c.ThirdName == item.Value);
                }

                if (item.Key == "FourthName")
                {
                    query = query.Where(c => c.FourthName == item.Value);
                }

                if (item.Key == "Date")
                {
                    DateTime myDate;
                    myDate = Convert.ToDateTime(item.Value);
                    query = query.Where(c => c.Date == myDate);
                }

                if (item.Key == "Status")
                {
                    int Status;
                    Status = Convert.ToInt16 (item.Value);
                    query = query.Where(c => c.Status == Status);
                }


                if (item.Key == "Campus_Id")
                {
                    long campusid;
                    campusid = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Campus_Id == campusid);
                }



                if (item.Key == "Certificate_Id")
                {
                    long Certificateid;
                    Certificateid = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Certificate_Id == Certificateid);
                }



                if (item.Key == "Stage_Id")
                {
                    long stageid;
                    stageid = Convert.ToInt64(item.Value);
                   query = query.Where(c => c.Stage_Id == stageid);
                }



                if (item.Key == "Grade_Id")
                {
                    long gradeid;
                    gradeid = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Grade_Id == gradeid);
                }


                
            }
            return query.ToList();
        }

        public static void Update(ADM_Transfer_Request transferrequest)
        {

            ADM_Transfer_Request c = db.ADM_Transfer_Request.Single(x => x.Id  == transferrequest.Id );
            c.Payment_Order_Number = transferrequest.Payment_Order_Number;
            c.Price = transferrequest.Price;

            c.FirstName = transferrequest.FirstName;
            c.SecondName = transferrequest.SecondName;
            c.ThirdName = transferrequest.ThirdName;
            c.FourthName = transferrequest.FourthName;
            c.Date = transferrequest.Date;
            c.Campus_Id = transferrequest.Campus_Id;
            c.Certificate_Id = transferrequest.Certificate_Id;
            c.Stage_Id = transferrequest.Stage_Id;
            c.Grade_Id = transferrequest.Grade_Id;
            c.Status = transferrequest.Status;
            db.SaveChanges();

        }


        public static void UpdateStatus(ADM_Transfer_Request transferrequest)
        {

            ADM_Transfer_Request c = db.ADM_Transfer_Request.Single(x => x.Id == transferrequest.Id);
            c.Payment_Order_Number = transferrequest.Payment_Order_Number;
            c.Price = transferrequest.Price;

            c.Status = transferrequest.Status;
            db.SaveChanges();

        }

        public static Boolean Delete(long Id)
        {
            try
            {
                db.ADM_Transfer_Request.DeleteObject(db.ADM_Transfer_Request.Single(x => x.Id == Id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public static long Expen_ID(string screen)
        {

            var expenses = from x in db.ACC_Expenses_Screens where x.Screen_Id == (from y in db.SEC_Screen where y.Code == screen select y.Id).FirstOrDefault() select x;
            long e = expenses.FirstOrDefault().Expense_Id;
            return e;
        }


    }
}
